package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ISysConfigService;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 注册验证
 * 
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;


    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    @GetMapping("/openRegisterUser")
    public AjaxResult openRegisterUser(HttpServletResponse response) throws IOException
    {
        AjaxResult ajax = AjaxResult.success();
        String openRegisterUser = configService.selectConfigByKey("sys.account.registerUser");
        if (StringUtils.isEmpty(openRegisterUser))
        {
            ajax.put("openRegisterUser", true);
        }
        else{
            ajax.put("openRegisterUser", Convert.toBool(openRegisterUser));
        }
        return ajax;
    }

    @GetMapping("/getOpenIdByCode")
    public AjaxResult getOpenIdByCode(String code) throws IOException
    {
        //实际应用中应该在配置文件中配置上appid及secret，这里直接赋值，这两个值在微信公众平台获取
        String appid = configService.selectConfigByKey("sys.wechat.appid");
        String appsecret = configService.selectConfigByKey("sys.wechat.appsecret");
        //String appid = "wxb1a5820168bf729d";
        //String appsecret = "223e8ad373129fadb03469d405fdca24";
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";

        String rspStr = HttpUtils.sendGet(url, null, Constants.UTF8);
        if (StringUtils.isEmpty(rspStr))
        {

        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("openid", rspStr);
        return ajax;
    }
}
